import cheerio from 'cheerio'

let ProductListString = function (string) {
    let $ = cheerio.load(string),
        data = [];
    $('section.listW').each((index, item) => {
        data.push({
            id: $(item).find('sup').attr('no'),
            PnameN: $(item).find('.topics_pnameN').text(),
            imgSrc: $(item).find('img').attr('data-original'),
            price: $(item).find('.price').text(),
            delPrice: $(item).find('.delPrice').text(),
            aHerf: $(item).find('a').attr('href')
        })
    })
    return data.length ? data : null;
}

let DetailsString = function (string) {
    let $ = cheerio.load(string),
        data = {
            'title': $('.titleW h1').text(),
            'sellProPoint': $('.sellProPoint').text(),
            'price': $('.proSalePrice .price').text(),
            'oldprice': $('.marketprice del').text(),
            'log': $('.brandLink img').attr('src'),
            'brand': $('.brandLink a p')[0].children[1].data
        };

    data['swiperSmall'] = []
    $('.swiperSmall .swiper-slide img').each((i, item) => data['swiperSmall'].push($(item).attr('src')))

    data['parameter'] = []
    $('#prodesc_lst li').each((i, item) => data['parameter'].push($(item).text()))

    data['size'] = []
    $('.prodtl_tbl tbody tr').each((i, item) => {
        data['size'][i] = []
        $(item).find('td').each((index, item) => data['size'][i].push($(item).text()))
    })

    data['imgDetails'] = []
    $('.divsion_imageList li img').each((i, item) => {
        let src = $(item).attr('data-original')
        src && data['imgDetails'].push(src)
    })

    console.log(data)
}

export {
    ProductListString,
    DetailsString
}